def add_ball(space):
    mass = 1
    radius = 4
    inertia = pm.moment_for_circle(mass, 0, radius, vec2d(0,0)) # 1
    body = pm.Body(mass, inertia) # 2
    x = random.randint(0,600)
    body.position = x, 100 # 3
    shape = pm.Circle(body, radius, vec2d(0,0)) # 4
    space.add(body, shape) # 5
    return shape
"""
   1. All bodies must have their moment of inertia set. If our object is a normal ball we can use the predefined function moment_for_circle to calculate it given its mass and radius.
   2. After we have the inertia we can create the body of the ball.
   3. And we set its position
   4. And in order for it to collide with things, it needs to have one (or many) collision shape(s).
   5. Finally we add the body and shape to the space to include it in our simulation. 

Now that we can create balls we want to display them:
"""
def draw_ball(screen, ball):
    p = int(ball.body.position.x), 90-int(ball.body.position.y)
    pygame.draw.circle(screen, THECOLORS["grey"], p, int(ball.radius), 0)
    
"""
As I have used pygame in this example, we can use the draw.circle function to draw the balls. But first we must convert the position of the ball. We earlier set the gravity to -900 (that is, it will point down the y axis). Pygame things 0,0 is at the top left of the screen, with y increasing downwards. So we make a simlpe conversion of the y value.

With these two functions and a little code to spawn balls you should see a couple of balls falling. Yay!
"""
import sys, random
import pygame
from pygame.locals import *
from pygame.color import *
import pymunk as pm
from pymunk.vec2d import vec2d

#def add_ball(space):
#def draw_ball(screen, ball):

def flipy(y):
    """Small hack to convert chipmunk physics to pygame coordinates"""
    return -y

def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 490))
    pygame.display.set_caption("Test")
    clock = pygame.time.Clock()
    running = True
    
    pm.init_pymunk()
    space = pm.Space()
    space.gravity = vec2d(0.0, -150.0)
   # space.set_damping(1.0)

    body = pm.Body(pm.inf, pm.inf)
    body.position = vec2d(0,90)    
    l1 = pm.Segment(body, vec2d(0, flipy(90)), vec2d(600, flipy(90)), 1.0)
    l2 = pm.Segment(body, vec2d(600, flipy(90)), vec2d(600, flipy(0)), 0.0)
    l3 = pm.Segment(body, vec2d(0, flipy(90)), vec2d(0, flipy(0)), 0.0)
    l4 = pm.Segment(body, vec2d(100, flipy(90)), vec2d(200, flipy(45)), 0.0)
    l5 = pm.Segment(body, vec2d(200, flipy(45)), vec2d(300, flipy(90)), 0.0)
    space.add_static(l1, l2, l3, l4, l5)

    
    balls = []
    
    ticks_to_next_ball = 2
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                running = False
        
        ticks_to_next_ball -= 1
        if ticks_to_next_ball <= 0:
            ticks_to_next_ball = 2
            ball_shape = add_ball(space)
            balls.append(ball_shape)

        screen.fill(THECOLORS["white"])
        
        for ball in balls:
            draw_ball(screen, ball)
        
        space.step(1/50.0)
        
        pygame.display.flip()
        clock.tick(50)
        
if __name__ == '__main__':
    sys.exit(main())   

